﻿using AdminStyem.Application.Command.traceabilityCommand;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using AutoMapper;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.traceabilityCommandHandler
{
    public class TraceabilityupdateCommandHandler : IRequestHandler<TraceabilityupdateCommand, APIResult<object>>
    {
        IBaseRespository<Traceability> BaseTranm;
        IMapper mapper;
        public TraceabilityupdateCommandHandler(IBaseRespository<Traceability> BaseTranm, IMapper mapper)
        {
            this.BaseTranm = BaseTranm;
            this.mapper = mapper;
        }
        public Task<APIResult<object>> Handle(TraceabilityupdateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<object> ret = new APIResult<object>();
                ret.Code = ResultCode.修改成功;
                ret.Message = ResultCode.修改成功.ToString();

                Traceability traceability = mapper.Map<Traceability>(request);
                traceability.TraceabilityId = request.TraceabilityId;
                traceability.Manufacturers = request.Manufacturers;
                traceability.TraceabilityPhoto = request.TraceabilityPhoto;
                traceability.TraceabilityShelf = request.TraceabilityShelf;
                traceability.TraceaDocuments = request.TraceaDocuments;
                traceability.CommodityId = request.CommodityId;
                BaseTranm.update(traceability);
                return Task.FromResult(ret);
            }
            catch (Exception)
            {
                throw;
                
            }
          
        }
    }
}
